﻿var lifecycle = {};

(function () {

    "use strict";

    // public functions

    lifecycle.process = function () {

        Utils.initialize();

        initStores();

        var localSettings = Windows.Storage.ApplicationData.current.localSettings.values;

        if (!localSettings['firstInstall']) {
            localSettings['firstInstall'] = true;
            firstInstall();
        } else {
            var _runningVersion = Utils.string2version(localSettings['runningVersion']);
            var _currentVersion = Windows.ApplicationModel.Package.current.id.version;

            if (_currentVersion.major > _runningVersion.major ||
                _currentVersion.minor > _runningVersion.minor ||
                _currentVersion.build > _runningVersion.build ||
                _currentVersion.revision > _runningVersion.revision) {
                localSettings['runningVersion'] = Utils.version2string(_currentVersion);
                newVersionFirstLaunch();
            }
        }

        Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", pause, false);
        Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", resume, false);

        appStart();
    };

    // private properties

    var defaultCredentials = [{
        name: 'facebook',
        link: 'www.facebook.com',
        icon: '/images/fb.jpg',
        category: 'Web',
        empty: true
    }, {
        name: 'twitter',
        link: 'www.twitter.com',
        icon: '/images/fb.jpg',
        category: 'Web',
        empty: true
    }, {
        name: 'flickr',
        link: 'www.flickr.com',
        icon: '/images/fb.jpg',
        category: 'Web',
        empty: true
    }, {
        name: 'GMail',
        link: 'mail.google.com',
        icon: '/images/fb.jpg',
        category: 'mail',
        empty: true
    }, {
        name: 'Hotmail',
        link: 'www.hotmail.com',
        icon: '/images/fb.jpg',
        category: 'mail',
        empty: true
    }, {
        name: 'Yahoo Mail',
        link: 'mail.yahoo.com',
        icon: '/images/fb.jpg',
        category: 'mail',
        empty: true
    }, {
        name: 'Bank of America',
        link: 'www.bankofamerica.com',
        icon: '/images/fb.jpg',
        category: 'Bank',
        empty: true
    }, {
        name: 'ICBC',
        link: 'www.icbc.com.cn',
        icon: '/images/fb.jpg',
        category: 'Bank',
        empty: true
    }, {
        name: 'Citi Bank',
        link: 'citi.com',
        icon: '/images/fb.jpg',
        category: 'Bank',
        empty: true
    }];

    // private functions

    function firstInstall() {
        console.log('firstInstall');
        initDefaulCredentials();
    }

    function newVersionFirstLaunch() {
        console.log('newVersionFirstLaunch');
    }

    function appStart() {
        Utils.log('appStart');
    }

    function pause() {
        console.log('pause');
    }

    function resume() {
        console.log('resume');
    }

    function initStores() {
        (new SafeBox.Store.UserStore({ model: 'SafeBox.Model.User' })).load();
        (new SafeBox.Store.CredentialStore({ model: 'SafeBox.Model.Credential' })).load();
    }

    function initDefaulCredentials() {
        var _credentialStore = Ext.StoreMgr.lookup('SafeBox.Store.CredentialStore');
        var i, _c;

        for (i = 0; i < defaultCredentials.length; i++) {
            _c = defaultCredentials[i];
            _credentialStore.add({ name: _c.name, link: _c.link, category: _c.category, empty: _c.empty, icon: _c.icon });
        }
        _credentialStore.sync();
    }
})();